// Copyright (c) 2021 Presto Labs Pte. Ltd.
// Author: hyuan

#pragma once

#include <string>

#include <rapidjson/document.h>

#include "coin/proto/coin_query.pb.h"

namespace coin2::exchange::btcturk::order_v1 {

namespace error_code {

inline int32_t GetErrorCodeAsInt(
    const rapidjson::GenericValue<rapidjson::UTF8<>>& json_error_code) {
  int32_t error_code = 0;
  if (json_error_code.IsInt()) {
    error_code = json_error_code.GetInt();
  } else if (json_error_code.IsString() && std::strlen(json_error_code.GetString()) > 0) {
    error_code = std::stoi(json_error_code.GetString());
  }
  return error_code;
}

inline std::string GetExchangeOrderErrorCode(
    const rapidjson::GenericValue<rapidjson::UTF8<>>& json) {
  if (json.HasMember("code")) {
    return std::to_string(GetErrorCodeAsInt(json["code"]));
  } else {
    return {};
  }
}

inline std::string GetExchangeOrderErrorMsg(
    const rapidjson::GenericValue<rapidjson::UTF8<>>& json) {
  if (json.HasMember("message")) {
    return json["message"].GetString();
  } else {
    return {};
  }
}

inline coin::proto::OrderErrorCode GetOrderErrorCode(
    const rapidjson::GenericValue<rapidjson::UTF8<>>& json_success,
    const rapidjson::GenericValue<rapidjson::UTF8<>>& json_error_code) {
  using coin::proto::OrderErrorCode;
  auto result = coin::proto::OrderErrorCode::ORDER_UNKNOWN_ERROR;
  int32_t error_code = GetErrorCodeAsInt(json_error_code);

  if ((json_success.IsBool() && json_success.IsTrue()) ||
      (error_code == 0)) {
    result = OrderErrorCode::ORDER_NO_ERROR;
  } else if (error_code == 1051) {
    result = OrderErrorCode::ORDER_FILLED_ALREADY;
  } else if (error_code == 1052) {
    result = OrderErrorCode::ORDER_NOT_FOUND;
  }

  return result;
}

}  // namespace error_code

}  // namespace coin2::exchange::btcturk::order_v1
